/**
 * 
 */
package au.edu.swin.videoshop.posapp.ui.sections;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.ejb.EJB;
import javax.faces.component.UIData;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

import au.edu.swin.videoshop.bean.Classification;
import au.edu.swin.videoshop.bean.MediumType;
import au.edu.swin.videoshop.bean.Store;
import au.edu.swin.videoshop.bean.Title;
import au.edu.swin.videoshop.bean.TitleCategory;
import au.edu.swin.videoshop.bean.TitleCopy;
import au.edu.swin.videoshop.bean.TitleMedium;
import au.edu.swin.videoshop.bean.TitleSearchCriteria;
import au.edu.swin.videoshop.service.StoreService;
import au.edu.swin.videoshop.service.TitleService;

/**
 * @author julie
 *
 */
public class TitleSection {
	private String mMode;
	
	private List mClassifications;
	private List mMediumTypes;
	
	private List mCategories;
	private List mTitles;
	private TitleSearchCriteria mSearchCriteria;
	
	private Title mSelectedTitle;
	private List mSelectedTitleCopies;
	
	private String mNewTitleCopyId;
	private Long mNewTitleCopyStoreId;;
	private String mNewTitleCopyMediumType;
	
	private List mNewTitleCopyAvailableStores;
	private List mNewTitleCopyAvailableMediumType;

	@EJB
	private TitleService mTitleService;
	
	@EJB
	private StoreService mStoreService;
	
	public TitleSection(){
		mSearchCriteria = new TitleSearchCriteria();
	}
	
	/**
	 * @return the searchCriteria
	 */
	public TitleSearchCriteria getSearchCriteria() {
		return mSearchCriteria;
	}

	/**
	 * @return the categories
	 */
	public List getSearchCategories() {
		if (mCategories == null) {
			mCategories = new ArrayList();
			mCategories.add(new SelectItem(null, "All categories"));
			List cats = mTitleService.findAllCategories();
			for (Iterator i = cats.iterator(); i.hasNext();) {
				TitleCategory cat = (TitleCategory)i.next();
				mCategories.add(new SelectItem(cat.getTitleCategoryId(), cat.getCategoryName()));
			}
		}
		return mCategories;
	}
	
	/**
	 * @return the categories
	 */
	public List getCategories() {
		if (mCategories == null) {
			mCategories = new ArrayList();
			List cats = mTitleService.findAllCategories();
			for (Iterator i = cats.iterator(); i.hasNext();) {
				TitleCategory cat = (TitleCategory)i.next();
				mCategories.add(new SelectItem(cat.getTitleCategoryId(), cat.getCategoryName()));
			}
		}
		return mCategories;
	}
	
	/**
	 * @return the classifications
	 */
	public List getClassifications() {
		if (mClassifications == null) {
			mClassifications = new ArrayList();
			mClassifications.add(new SelectItem(Classification.G.getName(), Classification.G.getName()));
			mClassifications.add(new SelectItem(Classification.PG.getName(), Classification.PG.getName()));
			mClassifications.add(new SelectItem(Classification.M.getName(), Classification.M.getName()));
			mClassifications.add(new SelectItem(Classification.MA.getName(), Classification.MA.getName()));
			mClassifications.add(new SelectItem(Classification.R.getName(), Classification.R.getName()));
			mClassifications.add(new SelectItem(Classification.X.getName(), Classification.X.getName()));
			
		}
		return mClassifications;
	}
	
	/**
	 * @return the mediumTypes
	 */
	public List getMediumTypes() {
		if (mMediumTypes == null) {
			mMediumTypes = new ArrayList();
			mMediumTypes.add(new SelectItem(MediumType.DVD.getName(), MediumType.DVD.getDescription()));
			mMediumTypes.add(new SelectItem(MediumType.BLURAY.getName(), MediumType.BLURAY.getDescription()));
			mMediumTypes.add(new SelectItem(MediumType.HDDVD.getName(), MediumType.HDDVD.getDescription()));
			mMediumTypes.add(new SelectItem(MediumType.VCD.getName(), MediumType.VCD.getDescription()));
			mMediumTypes.add(new SelectItem(MediumType.TAPE.getName(), MediumType.TAPE.getDescription()));
			
		}
		return mMediumTypes;
	}

	/**
	 * @return the titles
	 */
	public List getTitles() {
		if (mTitles == null) {
			refreshTitles();
		}
		return mTitles;
	}

	/**
	 * @return the selectedTitle
	 */
	public Title getSelectedTitle() {
		return mSelectedTitle;
	}

	/**
	 * @return the selectedTitleClassification
	 */
	public String getSelectedTitleClassification() {
		if (mSelectedTitle != null && mSelectedTitle.getClassification() != null) {
			return mSelectedTitle.getClassification().getName();
		}
		return null;
	}

	/**
	 * @param selectedTitleClassification the selectedTitleClassification to set
	 */
	public void setSelectedTitleClassification(String selectedTitleClassification) {
		mSelectedTitle.setClassification((Classification.getInstance(selectedTitleClassification)));
	}
	

	/**
	 * @return the selectedTitleCategory
	 */
	public Long getSelectedTitleCategory() {
		if (mSelectedTitle != null && mSelectedTitle.getCategory() != null) {
			return mSelectedTitle.getCategory().getTitleCategoryId();
		}
		return null;
	}

	/**
	 * @param selectedTitleCategory the selectedTitleCategory to set
	 */
	public void setSelectedTitleCategory(Long selectedTitleCategory) {
		mSelectedTitle.setCategory(mTitleService.findCategoryByTilteCategoryId(selectedTitleCategory));

	}

	/**
	 * @return the selectedTitleCopies
	 */
	public List getSelectedTitleCopies() {
		return mSelectedTitleCopies;
	}

	/**
	 * @return the newTitleCopyAvailableMediumType
	 */
	public List getNewTitleCopyAvailableMediumType() {
		return mNewTitleCopyAvailableMediumType;
	}

	/**
	 * @return the newTitleCopyId
	 */
	public String getNewTitleCopyId() {
		return mNewTitleCopyId;
	}

	/**
	 * @return the newTitleCopyAvailableStores
	 */
	public List getNewTitleCopyAvailableStores() {
		if (mNewTitleCopyAvailableStores == null) {
			FacesContext facesContext = FacesContext.getCurrentInstance();
			UserSection userSection = (UserSection)facesContext.getApplication().evaluateExpressionGet(facesContext, "#{userSection}", UserSection.class);

			if (userSection.getStaff().isSeniorManager()) {
				List stores = mStoreService.findAllStores();
				mNewTitleCopyAvailableStores = new ArrayList();
				for (Iterator i = stores.iterator(); i.hasNext();) {
					Store store = (Store) i.next();
					mNewTitleCopyAvailableStores.add(new SelectItem(store.getStoreId(), store.getStoreName()));
				}
			}
			else {
				mNewTitleCopyAvailableStores = new ArrayList();
				mNewTitleCopyAvailableStores.add(new SelectItem(userSection.getStaff().getStore().getStoreId(), userSection.getStaff().getStore().getStoreName()));
			}
		}
		return mNewTitleCopyAvailableStores;
	}

	/**
	 * @param newTitleCopyId the newTitleCopyId to set
	 */
	public void setNewTitleCopyId(String newTitleCopyId) {
		mNewTitleCopyId = newTitleCopyId;
	}

	/**
	 * @return the newTitleCopyStoreId
	 */
	public Long getNewTitleCopyStoreId() {
		return mNewTitleCopyStoreId;
	}

	/**
	 * @return the newTitleCopyMediumType
	 */
	public String getNewTitleCopyMediumType() {
		return mNewTitleCopyMediumType;
	}

	/**
	 * @param newTitleCopyStoreId the newTitleCopyStoreId to set
	 */
	public void setNewTitleCopyStoreId(Long newTitleCopyStoreId) {
		mNewTitleCopyStoreId = newTitleCopyStoreId;
	}

	/**
	 * @param newTitleCopyMediumType the newTitleCopyMediumType to set
	 */
	public void setNewTitleCopyMediumType(String newTitleCopyMediumType) {
		mNewTitleCopyMediumType = newTitleCopyMediumType;
	}

	private void refreshTitles() {
		mTitles = mTitleService.findTitlesBySearchCriteria(mSearchCriteria);
	}
	
	private void refreshTitleCopies() {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		UserSection userSection = (UserSection)facesContext.getApplication().evaluateExpressionGet(facesContext, "#{userSection}", UserSection.class);

		Store selectedStore = userSection.getStaff().getStore();
		
		mNewTitleCopyAvailableMediumType = new ArrayList();
		mSelectedTitleCopies = new ArrayList();
		for (Iterator i = mSelectedTitle.getMedia().iterator(); i.hasNext();) {
			TitleMedium medium = (TitleMedium) i.next();
			
			List copies = medium.getCopies();
			for (Iterator j = copies.iterator(); j.hasNext();) {
				TitleCopy copy = (TitleCopy) j.next();
				
				if (selectedStore == null || copy.getStore().equals(selectedStore)) {
					mSelectedTitleCopies.add(copy);
				}
			}
			
			if (medium.isAvailable()) {
				mNewTitleCopyAvailableMediumType.add(new SelectItem(medium.getMediumType().getName(), medium.getMediumType().getDescription()));
			}
		}
		
		
	}
	
	public String selectCategory() {
		refreshTitles();
		return null;
	}
	
	public String editTitle() {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		UIViewRoot root = facesContext.getViewRoot();
		UIData table = (UIData)root.findComponent("titleForm").findComponent("titleTable");
		mSelectedTitle = (Title) table.getRowData();
		
		mMode = "edit";
		
		return "editTitle";
	}
	
	public String add() {
		mSelectedTitle = new Title();
		
		mSelectedTitle.addMedium(MediumType.TAPE);
		mSelectedTitle.addMedium(MediumType.VCD);
		mSelectedTitle.addMedium(MediumType.DVD);
		mSelectedTitle.addMedium(MediumType.BLURAY);
		mSelectedTitle.addMedium(MediumType.HDDVD);
		
		mMode = "add";
		
		return "add";
	}
	
	public String saveTitle() {
		if (mSelectedTitle != null) {
			
			if (mMode.equals("add")) {
				mTitleService.addTitle(mSelectedTitle);
			}
			else if (mMode.equals("edit")) {
				mTitleService.updateTitle(mSelectedTitle);
			}
			
			refreshTitles();
		}
		
		return "browse";
	}
	
	public String editTitleCopies() {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		UIViewRoot root = facesContext.getViewRoot();
		UIData table = (UIData)root.findComponent("titleForm").findComponent("titleTable");
		mSelectedTitle = (Title) table.getRowData();

		refreshTitleCopies();
		
		return "editTitleCopies";
	}
	
	public String addTitleCopy() {
		MediumType mediumType = MediumType.getInstance(mNewTitleCopyMediumType);
		
		for (Iterator i = mSelectedTitle.getMedia().iterator(); i.hasNext();) {
			TitleMedium medium = (TitleMedium) i.next();
		
			if (medium.getMediumType() == mediumType) {
				medium.addCopy(mNewTitleCopyId, mStoreService.findStoreByStoreId(mNewTitleCopyStoreId));
			}
		}
		
		mTitleService.updateTitle(mSelectedTitle);
		
		refreshTitleCopies();
		
		return null;
	}
	
	public String deleteTitleCopy() {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		UIViewRoot root = facesContext.getViewRoot();
		UIData table = (UIData)root.findComponent("titleCopyForm").findComponent("titleCopyTable");
		TitleCopy selectedTitleCopy = (TitleCopy) table.getRowData();

		for (Iterator i = mSelectedTitle.getMedia().iterator(); i.hasNext();) {
			TitleMedium medium = (TitleMedium) i.next();
		
			medium.removeCopy(selectedTitleCopy);
		}
		
		refreshTitleCopies();
		
		return null;
	}

}
